{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "4h BOLL UP \n",
    "    1h BOLL UP\n",
    "    1h BOLL MD\n",
    "        30m BOLL UP 3%\n",
    "        30m BOLL MD 2%\n",
    "        现价 x\n",
    "        30m BOLL DN -2%\n",
    "    1h BOLL DN\n",
    "4h BOLL MD\n",
    "4h BOLL DN\n",
    "\n",
    "4h 🟩\n",
    "1h 🟩\n",
    "30m 🟥\n",
    "30m MACD DIF DEA MACD\n",
    "    KDJ  D J \n",
    "    RSI >70🔥  ⬜ <30🧊\n",
    "\n",
    "🔥\n",
    "🧊\n",
    "Suggest None/Short/Close IF Position(>30m BOLL MD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "# api get indicator\n",
    "key = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbHVlIjoiNjdjNjhhMmY4MDZmZjE2NTFlNjhhYjg3IiwiaWF0IjoxNzQxMDY0OTA4LCJleHAiOjMzMjQ1NTI4OTA4fQ.KmUs_BbUDc0oehhHP2Wxlr-O835IXU4YNvGnJPwe1rQ\"\n",
    "import requests\n",
    "\n",
    "rate_time = 15  # 15s\n",
    "\n",
    "\n",
    "def get_data(interval=\"4h\"):\n",
    "    res = {}\n",
    "    url = \"https://api.taapi.io/bulk\"\n",
    "\n",
    "    payload = {\n",
    "        \"secret\": key,\n",
    "        \"construct\": [\n",
    "            {\n",
    "                \"exchange\": \"binance\",\n",
    "                \"symbol\": \"BTC/USDT\",\n",
    "                \"interval\": interval,\n",
    "                \"indicators\": [\n",
    "                    {\"id\": \"price\", \"indicator\": \"price\"},\n",
    "                    {\"id\": \"bbands\", \"indicator\": \"bbands\"},\n",
    "                    {\"id\": \"macd\", \"indicator\": \"macd\"},\n",
    "                    {\"id\": \"kdj\", \"indicator\": \"kdj\"},\n",
    "                    {\"id\": \"rsi\", \"indicator\": \"rsi\", \"period\": 6},\n",
    "                ],\n",
    "            }\n",
    "        ],\n",
    "    }\n",
    "    headers = {\"Content-Type\": \"application/json\"}\n",
    "\n",
    "    response = requests.post(url, json=payload, headers=headers, timeout=15)\n",
    "\n",
    "    # 检查响应状态码\n",
    "    if response.status_code != 200:\n",
    "        print(f\"请求失败，状态码: {response.status_code}\")\n",
    "        # 打印响应内容\n",
    "        print(response.text)\n",
    "        return res\n",
    "        # else:\n",
    "\n",
    "    try:\n",
    "        # 将响应内容解析为 Python 对象\n",
    "        json_data = response.json()\n",
    "    except ValueError:\n",
    "        print(\"响应内容不是有效的 JSON 格式。\")\n",
    "        return res\n",
    "    # indicator = response\n",
    "    # print(response.text)\n",
    "\n",
    "    data = json_data[\"data\"]\n",
    "    for item in data:\n",
    "        if item[\"indicator\"] == \"price\":\n",
    "            res[\"price\"] = item[\"result\"][\"value\"]\n",
    "        if item[\"indicator\"] == \"bbands\":\n",
    "            res[\"band_up\"] = item[\"result\"][\"valueUpperBand\"]\n",
    "            res[\"band_md\"] = item[\"result\"][\"valueMiddleBand\"]\n",
    "            res[\"band_dn\"] = item[\"result\"][\"valueLowerBand\"]\n",
    "        if item[\"indicator\"] == \"macd\":\n",
    "            res[\"dif\"] = item[\"result\"][\"valueMACD\"]\n",
    "            res[\"dea\"] = item[\"result\"][\"valueMACDSignal\"]\n",
    "            res[\"macd\"] = item[\"result\"][\"valueMACDHist\"]\n",
    "        if item[\"indicator\"] == \"kdj\":\n",
    "            res[\"k\"] = item[\"result\"][\"valueK\"]\n",
    "            res[\"d\"] = item[\"result\"][\"valueD\"]\n",
    "            res[\"j\"] = item[\"result\"][\"valueJ\"]\n",
    "        if item[\"indicator\"] == \"rsi\":\n",
    "            res[\"rsi\"] = item[\"result\"][\"value\"]\n",
    "\n",
    "    return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'price': 84122.35,\n",
       " 'band_up': 86219.36033623436,\n",
       " 'band_md': 84253.61200000001,\n",
       " 'band_dn': 82287.86366376566,\n",
       " 'dif': -1004.3578906035109,\n",
       " 'dea': -1222.3116240366721,\n",
       " 'macd': 217.95373343316123,\n",
       " 'k': 51.53213173018516,\n",
       " 'd': 40.083833308965296,\n",
       " 'j': 74.42872857262489,\n",
       " 'rsi': 54.975800299446085}"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# lazy_load:\n",
    "#     data4h = get_data(interval='4h')\n",
    "#     time.sleep(1)\n",
    "#     data1h = get_data(interval='1h')\n",
    "#     time.sleep(1)\n",
    "data30 = get_data(interval='30m')\n",
    "data30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'price': 83451.17,\n",
       " 'band_up': 93041.18227276666,\n",
       " 'band_md': 86790.69650000008,\n",
       " 'band_dn': 80540.2107272335,\n",
       " 'dif': -1854.3844183658075,\n",
       " 'dea': -1573.077018091608,\n",
       " 'macd': -281.3074002741996,\n",
       " 'k': 14.36430854143493,\n",
       " 'd': 13.867637020166065,\n",
       " 'j': 15.357651583972661,\n",
       " 'rsi': 30.545303829463364}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1h = get_data(interval=\"1h\")\n",
    "data1h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'price': 83575.06,\n",
       " 'band_up': 94511.01693074501,\n",
       " 'band_md': 87585.96999999986,\n",
       " 'band_dn': 80660.9230692547,\n",
       " 'dif': -154.96400093355624,\n",
       " 'dea': 413.8877131530959,\n",
       " 'macd': -568.8517140866521,\n",
       " 'k': 23.336772971986548,\n",
       " 'd': 41.609585403896645,\n",
       " 'j': -13.208851891833646,\n",
       " 'rsi': 26.164251428666624}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data4h = get_data(interval=\"4h\")\n",
    "data4h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'price': 84188.68,\n",
       " 'band_up': 94475.46618639881,\n",
       " 'band_md': 87616.65099999985,\n",
       " 'band_dn': 80757.83581360089,\n",
       " 'dif': -106.0142573438061,\n",
       " 'dea': 423.67766187104587,\n",
       " 'macd': -529.691919214852,\n",
       " 'rsi': 40.436388850080796,\n",
       " 'k': 24.968503241085614,\n",
       " 'd': 42.15349549359633,\n",
       " 'j': -9.401481263935821}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# json loads offline rsp\n",
    "import json\n",
    "\n",
    "f = open(\"taapi_rsp.json\", \"r\")\n",
    "rsp = json.loads(f.read())\n",
    "f.close\n",
    "# print(rsp)\n",
    "\n",
    "# print (rsp['data'][('indicator' == 'price')['result']['value']])\n",
    "# rsp[\"data\"].index((lambda x:x['indicator']=='price')())\n",
    "data = rsp[\"data\"]\n",
    "\n",
    "res = {\n",
    "    \"price\": 0.0,\n",
    "    \"band_up\": 0.0,\n",
    "    \"band_md\": 0.0,\n",
    "    \"band_dn\": 0.0,\n",
    "    \"dif\": 0.0,\n",
    "    \"dea\": 0.0,\n",
    "    \"macd\": 0.0,\n",
    "    \"rsi\": 0.0,\n",
    "}\n",
    "for item in data:\n",
    "    if item[\"indicator\"] == \"price\":\n",
    "        res[\"price\"] = item[\"result\"][\"value\"]\n",
    "    if item[\"indicator\"] == \"bbands\":\n",
    "        res[\"band_up\"] = item[\"result\"][\"valueUpperBand\"]\n",
    "        res[\"band_md\"] = item[\"result\"][\"valueMiddleBand\"]\n",
    "        res[\"band_dn\"] = item[\"result\"][\"valueLowerBand\"]\n",
    "    if item[\"indicator\"] == \"macd\":\n",
    "        res[\"dif\"] = item[\"result\"][\"valueMACD\"]\n",
    "        res[\"dea\"] = item[\"result\"][\"valueMACDSignal\"]\n",
    "        res[\"macd\"] = item[\"result\"][\"valueMACDHist\"]\n",
    "    if item[\"indicator\"] == \"kdj\":\n",
    "        res[\"k\"] = item[\"result\"][\"valueK\"]\n",
    "        res[\"d\"] = item[\"result\"][\"valueD\"]\n",
    "        res[\"j\"] = item[\"result\"][\"valueJ\"]\n",
    "    if item[\"indicator\"] == \"rsi\":\n",
    "        res[\"rsi\"] = item[\"result\"][\"value\"]\n",
    "\n",
    "    # res = {\n",
    "    #     \"price\" : [item[\"result\"] for item in data if item[\"indicator\"] == \"price\"][0][\"value\"],\n",
    "    #     \"band_up\" : [item[\"result\"] for item in data if item[\"indicator\"] == \"bbands\"][0][\"valueUpperBand\"],\n",
    "    #     \"band_md\" : [item[\"result\"] for item in data if item[\"indicator\"] == \"bbands\"][0][\"valueMiddleBand\"],\n",
    "    #     \"band_dn\" : [item[\"result\"] for item in data if item[\"indicator\"] == \"bbands\"][0][\"valueLowerBand\"],\n",
    "    #     \"dif\" : [item[\"result\"] for item in data if item[\"indicator\"] == \"macd\"][0][\"valueMACD\"],\n",
    "    #     \"dea\" : [item[\"result\"] for item in data if item[\"indicator\"] == \"macd\"][0][\"valueMACDSignal\"],\n",
    "    #     \"macd\" : [item[\"result\"] for item in data if item[\"indicator\"] == \"macd\"][0][\"valueMACDHist\"],\n",
    "    #     \"rsi\" : [item[\"result\"] for item in data if item[\"indicator\"] == \"rsi\"][0][\"value\"],\n",
    "    # }\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       "#T_85b33_row0_col0, #T_85b33_row0_col1, #T_85b33_row1_col0, #T_85b33_row1_col1, #T_85b33_row2_col0, #T_85b33_row2_col1, #T_85b33_row3_col0, #T_85b33_row3_col1, #T_85b33_row4_col0, #T_85b33_row4_col1, #T_85b33_row5_col0, #T_85b33_row5_col1, #T_85b33_row6_col0, #T_85b33_row6_col1, #T_85b33_row7_col0, #T_85b33_row7_col1, #T_85b33_row8_col0, #T_85b33_row8_col1, #T_85b33_row9_col0, #T_85b33_row9_col1, #T_85b33_row10_col0, #T_85b33_row10_col1, #T_85b33_row11_col0, #T_85b33_row11_col1, #T_85b33_row12_col0, #T_85b33_row12_col1, #T_85b33_row13_col0, #T_85b33_row13_col1, #T_85b33_row14_col0, #T_85b33_row14_col1, #T_85b33_row15_col0, #T_85b33_row15_col1, #T_85b33_row16_col0, #T_85b33_row16_col1, #T_85b33_row17_col0, #T_85b33_row17_col1, #T_85b33_row18_col0, #T_85b33_row18_col1, #T_85b33_row19_col0, #T_85b33_row19_col1, #T_85b33_row20_col0, #T_85b33_row20_col1, #T_85b33_row21_col0, #T_85b33_row21_col1, #T_85b33_row22_col0, #T_85b33_row22_col1 {\n",
       "  text-align: left;\n",
       "}\n",
       "</style>\n",
       "<table id=\"T_85b33\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th class=\"blank level0\" >&nbsp;</th>\n",
       "      <th id=\"T_85b33_level0_col0\" class=\"col_heading level0 col0\" >Price</th>\n",
       "      <th id=\"T_85b33_level0_col1\" class=\"col_heading level0 col1\" >Compare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row0\" class=\"row_heading level0 row0\" >Time</th>\n",
       "      <td id=\"T_85b33_row0_col0\" class=\"data row0 col0\" >2025-03-04 17:10:28</td>\n",
       "      <td id=\"T_85b33_row0_col1\" class=\"data row0 col1\" >None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row1\" class=\"row_heading level0 row1\" >Symbol</th>\n",
       "      <td id=\"T_85b33_row1_col0\" class=\"data row1 col0\" >BTC/USTD</td>\n",
       "      <td id=\"T_85b33_row1_col1\" class=\"data row1 col1\" >None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row2\" class=\"row_heading level0 row2\" >4h BOLL UP</th>\n",
       "      <td id=\"T_85b33_row2_col0\" class=\"data row2 col0\" >94511.0</td>\n",
       "      <td id=\"T_85b33_row2_col1\" class=\"data row2 col1\" >8.78%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row3\" class=\"row_heading level0 row3\" >1h BOLL UP</th>\n",
       "      <td id=\"T_85b33_row3_col0\" class=\"data row3 col0\" >93041.2</td>\n",
       "      <td id=\"T_85b33_row3_col1\" class=\"data row3 col1\" >7.34%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row4\" class=\"row_heading level0 row4\" >4h BOLL MD</th>\n",
       "      <td id=\"T_85b33_row4_col0\" class=\"data row4 col0\" >87586.0</td>\n",
       "      <td id=\"T_85b33_row4_col1\" class=\"data row4 col1\" >1.57%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row5\" class=\"row_heading level0 row5\" >1h BOLL MD</th>\n",
       "      <td id=\"T_85b33_row5_col0\" class=\"data row5 col0\" >86790.7</td>\n",
       "      <td id=\"T_85b33_row5_col1\" class=\"data row5 col1\" >0.66%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row6\" class=\"row_heading level0 row6\" >30m BOLL UP</th>\n",
       "      <td id=\"T_85b33_row6_col0\" class=\"data row6 col0\" >86213.9</td>\n",
       "      <td id=\"T_85b33_row6_col1\" class=\"data row6 col1\" >0.00%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row7\" class=\"row_heading level0 row7\" >-></th>\n",
       "      <td id=\"T_85b33_row7_col0\" class=\"data row7 col0\" >86213.9</td>\n",
       "      <td id=\"T_85b33_row7_col1\" class=\"data row7 col1\" >None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row8\" class=\"row_heading level0 row8\" >30m BOLL MD</th>\n",
       "      <td id=\"T_85b33_row8_col0\" class=\"data row8 col0\" >84243.4</td>\n",
       "      <td id=\"T_85b33_row8_col1\" class=\"data row8 col1\" >-2.34%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row9\" class=\"row_heading level0 row9\" >30m BOLL DN</th>\n",
       "      <td id=\"T_85b33_row9_col0\" class=\"data row9 col0\" >82272.8</td>\n",
       "      <td id=\"T_85b33_row9_col1\" class=\"data row9 col1\" >-4.79%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row10\" class=\"row_heading level0 row10\" >4h BOLL DN</th>\n",
       "      <td id=\"T_85b33_row10_col0\" class=\"data row10 col0\" >80660.9</td>\n",
       "      <td id=\"T_85b33_row10_col1\" class=\"data row10 col1\" >-6.88%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row11\" class=\"row_heading level0 row11\" >1h BOLL DN</th>\n",
       "      <td id=\"T_85b33_row11_col0\" class=\"data row11 col0\" >80540.2</td>\n",
       "      <td id=\"T_85b33_row11_col1\" class=\"data row11 col1\" >-7.04%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row12\" class=\"row_heading level0 row12\" >4h</th>\n",
       "      <td id=\"T_85b33_row12_col0\" class=\"data row12 col0\" >🟩</td>\n",
       "      <td id=\"T_85b33_row12_col1\" class=\"data row12 col1\" >None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row13\" class=\"row_heading level0 row13\" >1h</th>\n",
       "      <td id=\"T_85b33_row13_col0\" class=\"data row13 col0\" >🟩</td>\n",
       "      <td id=\"T_85b33_row13_col1\" class=\"data row13 col1\" >None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row14\" class=\"row_heading level0 row14\" >30m</th>\n",
       "      <td id=\"T_85b33_row14_col0\" class=\"data row14 col0\" >🟥</td>\n",
       "      <td id=\"T_85b33_row14_col1\" class=\"data row14 col1\" >None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row15\" class=\"row_heading level0 row15\" >30m MACD</th>\n",
       "      <td id=\"T_85b33_row15_col0\" class=\"data row15 col0\" >DIF:-1020.7 DEA:-1225.6 MACD:204.9</td>\n",
       "      <td id=\"T_85b33_row15_col1\" class=\"data row15 col1\" >None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row16\" class=\"row_heading level0 row16\" >30m KDJ</th>\n",
       "      <td id=\"T_85b33_row16_col0\" class=\"data row16 col0\" >D:38.6 J:64.1</td>\n",
       "      <td id=\"T_85b33_row16_col1\" class=\"data row16 col1\" >None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row17\" class=\"row_heading level0 row17\" >30m RSI</th>\n",
       "      <td id=\"T_85b33_row17_col0\" class=\"data row17 col0\" >49.7 ⬜</td>\n",
       "      <td id=\"T_85b33_row17_col1\" class=\"data row17 col1\" >None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row18\" class=\"row_heading level0 row18\" >30m Suggest</th>\n",
       "      <td id=\"T_85b33_row18_col0\" class=\"data row18 col0\" >None/Short/Close</td>\n",
       "      <td id=\"T_85b33_row18_col1\" class=\"data row18 col1\" >None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row19\" class=\"row_heading level0 row19\" >1h MACD</th>\n",
       "      <td id=\"T_85b33_row19_col0\" class=\"data row19 col0\" >DIF:-1020.7 DEA:-1225.6 MACD:204.9</td>\n",
       "      <td id=\"T_85b33_row19_col1\" class=\"data row19 col1\" >None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row20\" class=\"row_heading level0 row20\" >1h KDJ</th>\n",
       "      <td id=\"T_85b33_row20_col0\" class=\"data row20 col0\" >D:38.6 J:64.1</td>\n",
       "      <td id=\"T_85b33_row20_col1\" class=\"data row20 col1\" >None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row21\" class=\"row_heading level0 row21\" >1h RSI</th>\n",
       "      <td id=\"T_85b33_row21_col0\" class=\"data row21 col0\" >49.7 ⬜</td>\n",
       "      <td id=\"T_85b33_row21_col1\" class=\"data row21 col1\" >None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_85b33_level0_row22\" class=\"row_heading level0 row22\" >1h Suggest</th>\n",
       "      <td id=\"T_85b33_row22_col0\" class=\"data row22 col0\" >None/Short/Close</td>\n",
       "      <td id=\"T_85b33_row22_col1\" class=\"data row22 col1\" >None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x11d9ad810>"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "\n",
    "def long_profit(open_at, now):\n",
    "    return now / open_at - 1\n",
    "\n",
    "\n",
    "# 做空\n",
    "def short_profit(open_at, now):\n",
    "    return 1 - now / open_at\n",
    "\n",
    "# 获取当前时间对象\n",
    "now = datetime.now()\n",
    "\n",
    "# 格式化输出（示例：2025-03-04 10:48:57）\n",
    "formatted_time = now.strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "# print(formatted_time)\n",
    "\n",
    "red = \"🟥\"\n",
    "green = \"🟩\"\n",
    "fire = \"🔥\"\n",
    "normal = \"⬜\"\n",
    "ice = \"🧊\"\n",
    "over_buy = 70\n",
    "over_sell = 30\n",
    "\n",
    "symbol = \"BTC/USTD\"\n",
    "\n",
    "top = {\n",
    "    \"Time\": [formatted_time],\n",
    "    \"Symbol\": [symbol],\n",
    "}\n",
    "\n",
    "curr = \"->\"\n",
    "mid = {\n",
    "    \"4h BOLL UP\": [data4h[\"band_up\"], \"1%\"],\n",
    "    \"1h BOLL UP\": [data1h[\"band_up\"], \"2%\"],\n",
    "    \"1h BOLL MD\": [data1h[\"band_md\"], \"3%\"],\n",
    "    \"30m BOLL UP\": [data30[\"band_up\"], \"4%\"],\n",
    "    \"30m BOLL MD\": [data30[\"band_md\"], \"5%\"],\n",
    "    curr: [data30[\"band_up\"]],\n",
    "    \"30m BOLL DN\": [data30[\"band_dn\"], \"7%\"],\n",
    "    \"1h BOLL DN\": [data1h[\"band_dn\"], \"8%\"],\n",
    "    \"4h BOLL MD\": [data4h[\"band_md\"], \"9%\"],\n",
    "    \"4h BOLL DN\": [data4h[\"band_dn\"], \"10%\"],\n",
    "}\n",
    "\n",
    "sorted_mid = sorted(mid.items(), key=lambda x: float(x[1][0]), reverse=True) \n",
    "\n",
    "now = mid[curr][0]\n",
    "for key in mid.keys():\n",
    "    if key == curr:\n",
    "      continue\n",
    "\n",
    "    if mid[key][0] > now:\n",
    "        mid[key][1] = f\"{short_profit(mid[key][0], now)*1e2:.2f}%\"\n",
    "    else:\n",
    "        mid[key][1] = f\"{short_profit(mid[key][0], now)*1e2:.2f}%\"\n",
    "    mid[key][0] = f\"{mid[key][0]:.1f}\"\n",
    "mid[curr][0] = f\"{mid[curr][0]:.1f}\"\n",
    "\n",
    "cycle = {\n",
    "    \"4h\": [f\"{red if data4h['macd'] > 0 else green}\"],\n",
    "    \"1h\": [f\"{red if data1h['macd'] > 0 else green}\"],\n",
    "    \"30m\": [f\"{red if data30['macd'] > 0 else green}\"],\n",
    "}\n",
    "data_30m = {\n",
    "    \"30m MACD\": [f\"DIF:{data30['dif']:.1f} DEA:{data30['dea']:.1f} MACD:{data30['macd']:.1f}\"],\n",
    "    \"30m KDJ\": [f\"D:{data30['d']:.1f} J:{data30['j']:.1f}\"],\n",
    "    \"30m RSI\": [f\"{data30['rsi']:.1f} {fire if data30['rsi']>over_buy else ice if data30['rsi'] <over_sell else normal}\"],\n",
    "    \"30m Suggest\": [\"None/Short/Close\"],\n",
    "}\n",
    "data_1h = {\n",
    "    \"1h MACD\": [f\"DIF:{data30['dif']:.1f} DEA:{data30['dea']:.1f} MACD:{data30['macd']:.1f}\"],\n",
    "    \"1h KDJ\": [f\"D:{data30['d']:.1f} J:{data30['j']:.1f}\"],\n",
    "    \"1h RSI\": [f\"{data30['rsi']:.1f} {fire if data30['rsi']>over_buy else ice if data30['rsi'] <over_sell else normal}\"],\n",
    "    \"1h Suggest\": [\"None/Short/Close\"],\n",
    "}\n",
    "\n",
    "suggest = {}\n",
    "\n",
    "out = {}\n",
    "out.update(top)\n",
    "out.update(sorted_mid)\n",
    "out.update(cycle)\n",
    "out.update(data_30m)\n",
    "out.update(data_1h)\n",
    "out.update(suggest)\n",
    "\n",
    "\n",
    "# arr = np.array([out.values()]),\n",
    "# print(arr)\n",
    "# df = pd.DataFrame(data=np,index=out.keys(),columns=['a','b'])\n",
    "df = pd.DataFrame(data=out.values(), index=out.keys(), columns=[\"Price\", \"Compare\"])\n",
    "left_aligned_df = df.style.set_properties(**{\"text-align\": \"left\"})\n",
    "left_aligned_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         R (km)    mass (x 10^29 kg)\n",
      "-----  --------  -------------------\n",
      "  Sun    696000           1.9891e+09\n",
      "Earth      6371        5973.6\n",
      " Moon      1737          73.5\n",
      " Mars      3390         641.85\n"
     ]
    }
   ],
   "source": [
    "from tabulate import tabulate\n",
    "\n",
    "tb_data = [[\"Sun\", 696000, 1989100000], [\"Earth\", 6371, 5973.6]]\n",
    "tb_data.append([\"Moon\", 1737, 73.5])\n",
    "tb_data.append([\"Mars\", 3390, 641.85])\n",
    "tb = tabulate(\n",
    "    tb_data,\n",
    "    tablefmt=\"simple\",\n",
    "    colalign=(\"right\",),\n",
    "    stralign=\"left\",\n",
    "    headers=[\"\", \"R (km)\", \"mass (x 10^29 kg)\"],\n",
    ")\n",
    "print(tb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Price                               Compare\n",
      "-----------  ----------------------------------  ---------\n",
      "       Time  2025-03-04 16:07:24\n",
      "     Symbol  BTC/USTD\n",
      " 4h BOLL UP  94511.0                             7.82%\n",
      " 1h BOLL UP  93041.2                             6.36%\n",
      " 4h BOLL MD  87586.0                             0.53%\n",
      "30m BOLL UP  87121.9                             0.00%\n",
      "         ->  87121.9\n",
      " 1h BOLL MD  86790.7                             -0.38%\n",
      "30m BOLL MD  84671.3                             -2.89%\n",
      "30m BOLL DN  82220.6                             -5.96%\n",
      " 4h BOLL DN  80660.9                             -8.01%\n",
      " 1h BOLL DN  80540.2                             -8.17%\n",
      "         4h  🟩\n",
      "         1h  🟩\n",
      "        30m  🟥\n",
      "   30m MACD  DIF:-1196.5 DEA:-1352.0 MACD:155.6\n",
      "    30m KDJ  D:32.1 J:29.7\n",
      "    30m RSI  34.6 ⬜\n",
      "30m Suggest  None/Short/Close\n",
      "    1h MACD  DIF:-1196.5 DEA:-1352.0 MACD:155.6\n",
      "     1h KDJ  D:32.1 J:29.7\n",
      "     1h RSI  34.6 ⬜\n",
      " 1h Suggest  None/Short/Close\n"
     ]
    }
   ],
   "source": [
    "# \"https://api.day.app/2KzwDuZCgNSSqjqf6pRcZU/这里改成你自己的推送内容\"\n",
    "import tabulate\n",
    "\n",
    "tabulate.PRESERVE_WHITESPACE = True\n",
    "tabulate.WIDE_CHARS_MODE = True\n",
    "dfto = df.to_markdown(\n",
    "    tablefmt=\"simple\",\n",
    "    colalign=(\"right\",),\n",
    "    stralign=\"left\",\n",
    ")\n",
    "# dfto = dfto.replace(\"  \", \"    \")  # 替换所有空格\n",
    "print(dfto)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"code\":200,\"message\":\"success\",\"timestamp\":1741075773}\n"
     ]
    }
   ],
   "source": [
    "import urllib\n",
    "import urllib.parse\n",
    "\n",
    "# body = df.to_csv()\n",
    "body = dfto\n",
    "# params = urllib.parse.urlencode(html)\n",
    "# url = \"https://api.day.app/2KzwDuZCgNSSqjqf6pRcZU/%s\" % params\n",
    "\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "# 获取当前时间对象\n",
    "now = datetime.now()\n",
    "\n",
    "# 格式化输出（示例：2025-03-04 10:48:57）\n",
    "formatted_time = now.strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "# print(formatted_time)\n",
    "\n",
    "import requests\n",
    "import json\n",
    "\n",
    "key = \"2KzwDuZCgNSSqjqf6pRcZU\"\n",
    "url = \"https://api.day.app/%s\" % key\n",
    "\n",
    "data = {\n",
    "    \"body\": body,\n",
    "    \"title\": \"MJR WATCHER(%s)\" % formatted_time,\n",
    "    #   \"badge\": 1,\n",
    "    #   \"sound\": \"minuet\",\n",
    "    #   \"icon\": \"https://day.app/assets/images/avatar.jpg\",\n",
    "    #   \"group\": \"test\",\n",
    "    #   \"url\": \"https://mritd.com\"\n",
    "}\n",
    "# payload = urllib.parse.urlencode(data)\n",
    "payload = json.dumps(data)\n",
    "headers = {\"Content-Type\": \"application/json; charset=utf-8\"}\n",
    "\n",
    "# print(payload)\n",
    "response = requests.post(url, headers=headers, data=payload)\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-03-04 10:49:32\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "# 获取当前时间对象\n",
    "now = datetime.now()\n",
    "\n",
    "# 格式化输出（示例：2025-03-04 10:48:57）\n",
    "formatted_time = now.strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "print(formatted_time)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http://www.musi-cal.com/cgi-bin/query?spam=1&eggs=2&bacon=0\n"
     ]
    }
   ],
   "source": [
    "import urllib.parse\n",
    "\n",
    "params = urllib.parse.urlencode({\"spam\": 1, \"eggs\": 2, \"bacon\": 0})\n",
    "url = \"http://www.musi-cal.com/cgi-bin/query?%s\" % params\n",
    "print(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'time': 1741085337.572576}\n",
      "2025-03-04  18:48:57\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import pytz\n",
    "tres = {\"time\":time.time()}\n",
    "print(tres)\n",
    "\n",
    "def format_time(timestamp):\n",
    "    tz = pytz.timezone(\"Asia/Shanghai\")\n",
    "    return datetime.fromtimestamp(timestamp, tz).strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "\n",
    "print(format_time(tres['time']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "483633.0\n",
      "120908.0\n",
      "967267.0\n"
     ]
    }
   ],
   "source": [
    "cycle1h=1*3600\n",
    "cycle4h=4*3600\n",
    "cycle30m=30*60\n",
    "nt = time.time()\n",
    "print(nt//cycle1h)\n",
    "print(nt//cycle4h)\n",
    "print(nt//cycle30m)\n",
    "# //cycle1h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"time\": \"2025-03-04  18:50:44\"}\n",
      "1741085444.0\n"
     ]
    }
   ],
   "source": [
    "res1 = {\"time\": format_time(time.time())}\n",
    "jstr = json.dumps(res1)\n",
    "print(jstr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"time\": \"2025-03-05  10:09:02\"}\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import json\n",
    "from datetime import datetime\n",
    "\n",
    "\n",
    "def format_time(timestamp):\n",
    "    # 将时间戳转换为格式化的字符串（示例格式：2025-03-04 18:47:21）\n",
    "    return datetime.fromtimestamp(timestamp).strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "\n",
    "\n",
    "res1 = {\"time\": format_time(time.time())}\n",
    "jstr = json.dumps(res1)\n",
    "print(jstr)  # 输出示例：{\"time\": \"2025-03-04 18:47:21\"}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "datetime.fromtimestamp(time.time()).hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello你好  　　　　　║\n",
      "World!!　　　　　　　║\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "re_chinese = re.compile(\n",
    "    r\"[\\u4e00-\\u9fa5\\！\\？\\。\\＂\\＇\\（\\）\\＊\\＋\\，\\－\\／\\：\\；\\＜\\＝\\＞\\＠\\［\\＼\\］\\＾\\＿\\｀\\｛\\｜\\｝\\～\\｟\\｠\\、\\〃\\《\\》\\「\\」\\『\\』\\【\\】\\〔\\〕\\〖\\〗\\〘\\〙\\〚\\〛\\〜\\〝\\〞\\〟\\〰\\〾\\〿\\–\\—\\‘\\’\\‛\\“\\”\\„\\‟\\…\\‧\\﹏\\．]\", re.S)\n",
    "\n",
    "\n",
    "def format_ascii(text):\n",
    "    t = re.findall(re_chinese, text)\n",
    "    count = len(t)\n",
    "    return text + \" \" * count + u\"\\u3000\" * (len(text) - count)\n",
    "\n",
    "\n",
    "print(format_ascii(\"Hello你好\") + \"║\")\n",
    "print(format_ascii(\"World!!\") + \"║\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'two': 2, 'three': 3, 'four': 4}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {\"one\": 1, \"two\": 2, \"three\": 3, \"four\": 4}\n",
    "del d['one']\n",
    "d"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
